package com.lnking.lnkingplace.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lnking.lnkingplace.model.dto.placeType.PlaceTypeTreeDto;
import com.lnking.lnkingplace.model.entity.PlaceType;
import com.lnking.lnkingplace.mapper.PlaceTypeMapper;
import com.lnking.lnkingplace.model.request.placeProblem.PlaceProblemUpdateRequest;
import com.lnking.lnkingplace.model.request.placeType.PlaceTypeAddRequest;
import com.lnking.lnkingplace.model.request.placeType.PlaceTypeUpdateRequest;
import com.lnking.lnkingplace.service.IPlaceTypeService;
import com.lnking.starter.mybatis.service.impl.CustomServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhangqt
 * @since 2022-09-14
 */
@Service
public class PlaceTypeServiceImpl extends CustomServiceImpl<PlaceTypeMapper, PlaceType> implements IPlaceTypeService {

    @Override
    public List<PlaceTypeTreeDto> getTree() {
        List<PlaceTypeTreeDto> list = this.baseMapper.queryList();
        //处理树形
        List<PlaceTypeTreeDto> result = list.stream()
                .filter(entity -> entity.getPId().equals("0"))
                .peek(entity -> entity.setChildNode(getChildNode(entity, list))).collect(Collectors.toList());
        return result;
    }

    /**
     * 处理子类
     * @param root
     * @param allListTree
     * @return
     */
    private List<PlaceTypeTreeDto> getChildNode(PlaceTypeTreeDto root, List<PlaceTypeTreeDto> allListTree) {
        List<PlaceTypeTreeDto> collect = allListTree.stream()
                .filter(entity -> Objects.equals(entity.getPId(), root.getId()))
                .peek(entity -> entity.setChildNode(getChildNode(entity, allListTree))).collect(Collectors.toList());
        return collect;
    }

    @Override
    public void add(PlaceTypeAddRequest params) {
        String name = params.getName();
        PlaceType check = check(name);
        Assert.isTrue(null == check, "该类型已存在");
        PlaceType info = new PlaceType();
        BeanUtils.copyProperties(params,info);
        if(StringUtils.isBlank(params.getPId())){
            info.setPId("0");
        }
        info.setCreateTime(LocalDateTime.now());
        this.baseMapper.insert(info);
    }

    private PlaceType check(String name){
        LambdaQueryWrapper<PlaceType> query = new LambdaQueryWrapper<>();
        query.eq(PlaceType::getName,name);
        PlaceType placeType = this.baseMapper.selectOne(query);
        return placeType;
    }

    @Override
    public void update(PlaceTypeUpdateRequest params) {
        String name = params.getName();
        PlaceType check = check(name);
        Assert.isTrue(null == check ||
                check.getId().equals(params.getId()) , "该类型已存在");
        PlaceType info = new PlaceType();
        BeanUtils.copyProperties(params,info);
        this.baseMapper.updateById(info);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(String id) {
        //删除子节点
        this.baseMapper.updateDelFlag(id);
        //删除根节点
        this.removeById(id);
    }
}
